package com.maxwell.impl;

import com.maxwell.entity.Order;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.io.UnsupportedEncodingException;

/**
 * User:Maxwell
 * Date:2015/2/16
 * Time:14:46
 */
@Component
public class SimpleJavaSenderHelp {

    private static final Logger LOGGER = LoggerFactory.getLogger(SimpleJavaSenderHelp.class);

    @Autowired
    private JavaMailSender javaMailSender;

    @Value("${mail.user.name}")
    private String fromMail;

    public void placeOrder(Order order) {
        MimeMessage message = javaMailSender.createMimeMessage();
        MimeMessageHelper messageHelper = new MimeMessageHelper(message);
        try {
            messageHelper.setTo(new InternetAddress(order.getUserEmail(), order.getUserName()));
            messageHelper.setSubject(order.getSubject());
            messageHelper.setText("This is a MimeMessage Help demo!");
            messageHelper.setFrom(new InternetAddress(fromMail,"Maxwell 3"));
        } catch (MessagingException e) {
            LOGGER.error("simple java sender help error happened:{}",e.getMessage());
        } catch (UnsupportedEncodingException e) {
            LOGGER.error("simple java sender help error happened:{}", e.getMessage());
        }
        javaMailSender.send(message);
    }

    public void attachments(Order order){
        MimeMessage message = javaMailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message,true);
            helper.setFrom(fromMail);
            helper.setTo(order.getUserEmail());
            helper.setSubject(order.getSubject());
            helper.setText("This has a image,an attachment demo!!!");
            FileSystemResource fileSource = new FileSystemResource(new File("C:\\Users\\Public\\Pictures\\Sample Pictures\\80X80.jpg"));
            helper.addAttachment("Test.jpg",fileSource);
        } catch (MessagingException e) {
            LOGGER.error("simple java sender help error happened:{}", e.getMessage());
        }
        javaMailSender.send(message);
    }

    public void inlineResource(Order order){
        MimeMessage message = javaMailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message,true);
            helper.setTo(order.getUserEmail());
            helper.setText("<html><body><img src='cid:identifier1234'></body></html>",true);
            FileSystemResource fileSystemResource = new FileSystemResource(new File("C:\\Users\\Public\\Pictures\\Sample Pictures\\80X80.jpg"));
            helper.addInline("identifier1234",fileSystemResource);
            helper.setFrom(fromMail);
            helper.setSubject(order.getSubject());
        } catch (MessagingException e) {
            LOGGER.error("simple java sender help error happened:{}",e.getMessage());
        }
        javaMailSender.send(message);
    }



}
